home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 21 / AACD 21.iso / AACD / Utilities / Ghostscript / src / sjpegc.c < prev    next >
Encoding:
C/C++ Source or Header  |  2001-01-01  |  5.6 KB  |  216 lines

  1. /* Copyright (C) 1994, 1997, 1999 Aladdin Enterprises.  All rights reserved.
  2.   
  3.   This file is part of AFPL Ghostscript.
  4.   
  5.   AFPL Ghostscript is distributed with NO WARRANTY OF ANY KIND.  No author or
  6.   distributor accepts any responsibility for the consequences of using it, or
  7.   for whether it serves any particular purpose or works at all, unless he or
  8.   she says so in writing.  Refer to the Aladdin Free Public License (the
  9.   "License") for full details.
  10.   
  11.   Every copy of AFPL Ghostscript must include a copy of the License, normally
  12.   in a plain ASCII text file named PUBLIC.  The License grants you the right
  13.   to copy, modify and redistribute AFPL Ghostscript, but only under certain
  14.   conditions described in the License.  Among other things, the License
  15.   requires that the copyright notice and this notice be preserved on all
  16.   copies.
  17. */
  18.  
  19. /*$Id: sjpegc.c,v 1.2 2000/09/19 19:00:50 lpd Exp $ */
  20. /* Interface routines for IJG code, common to encode/decode. */
  21. #include "stdio_.h"
  22. #include "string_.h"
  23. #include "jpeglib_.h"
  24. #include "jerror_.h"
  25. #include "jmemsys.h"        /* for prototypes */
  26. #include "gx.h"
  27. #include "gserrors.h"
  28. #include "strimpl.h"
  29. #include "sdct.h"
  30. #include "sjpeg.h"
  31.  
  32. /*
  33.  * Error handling routines (these replace corresponding IJG routines from
  34.  * jpeg/jerror.c).  These are used for both compression and decompression.
  35.  * We assume
  36.  * offset_of(jpeg_compress_data, cinfo)==offset_of(jpeg_decompress_data, dinfo)
  37.  */
  38.  
  39. private void
  40. gs_jpeg_error_exit(j_common_ptr cinfo)
  41. {
  42.     jpeg_stream_data *jcomdp =
  43.     (jpeg_stream_data *) ((char *)cinfo -
  44.               offset_of(jpeg_compress_data, cinfo));
  45.  
  46.     longjmp(jcomdp->exit_jmpbuf, 1);
  47. }
  48.  
  49. private void
  50. gs_jpeg_emit_message(j_common_ptr cinfo, int msg_level)
  51. {
  52.     if (msg_level < 0) {    /* GS policy is to ignore IJG warnings when Picky=0,
  53.                  * treat them as errors when Picky=1.
  54.                  */
  55.     jpeg_stream_data *jcomdp =
  56.     (jpeg_stream_data *) ((char *)cinfo -
  57.                   offset_of(jpeg_compress_data, cinfo));
  58.  
  59.     if (jcomdp->Picky)
  60.         gs_jpeg_error_exit(cinfo);
  61.     }
  62.     /* Trace messages are always ignored. */
  63. }
  64.  
  65. /*
  66.  * This routine initializes the error manager fields in the JPEG object.
  67.  * It is based on jpeg_std_error from jpeg/jerror.c.
  68.  */
  69.  
  70. void
  71. gs_jpeg_error_setup(stream_DCT_state * st)
  72. {
  73.     struct jpeg_error_mgr *err = &st->data.common->err;
  74.  
  75.     /* Initialize the JPEG compression object with default error handling */
  76.     jpeg_std_error(err);
  77.  
  78.     /* Replace some methods with our own versions */
  79.     err->error_exit = gs_jpeg_error_exit;
  80.     err->emit_message = gs_jpeg_emit_message;
  81.  
  82.     st->data.compress->cinfo.err = err;        /* works for decompress case too */
  83. }
  84.  
  85. /* Stuff the IJG error message into errorinfo after an error exit. */
  86.  
  87. int
  88. gs_jpeg_log_error(stream_DCT_state * st)
  89. {
  90.     j_common_ptr cinfo = (j_common_ptr) & st->data.compress->cinfo;
  91.     char buffer[JMSG_LENGTH_MAX];
  92.  
  93.     /* Format the error message */
  94.     (*cinfo->err->format_message) (cinfo, buffer);
  95.     (*st->report_error) ((stream_state *) st, buffer);
  96.     return gs_error_ioerror;    /* caller will do return_error() */
  97. }
  98.  
  99.  
  100. /*
  101.  * Interface routines.  This layer of routines exists solely to limit
  102.  * side-effects from using setjmp.
  103.  */
  104.  
  105.  
  106. JQUANT_TBL *
  107. gs_jpeg_alloc_quant_table(stream_DCT_state * st)
  108. {
  109.     if (setjmp(st->data.common->exit_jmpbuf)) {
  110.     gs_jpeg_log_error(st);
  111.     return NULL;
  112.     }
  113.     return jpeg_alloc_quant_table((j_common_ptr)
  114.                   & st->data.compress->cinfo);
  115. }
  116.  
  117. JHUFF_TBL *
  118. gs_jpeg_alloc_huff_table(stream_DCT_state * st)
  119. {
  120.     if (setjmp(st->data.common->exit_jmpbuf)) {
  121.     gs_jpeg_log_error(st);
  122.     return NULL;
  123.     }
  124.     return jpeg_alloc_huff_table((j_common_ptr)
  125.                  & st->data.compress->cinfo);
  126. }
  127.  
  128. int
  129. gs_jpeg_destroy(stream_DCT_state * st)
  130. {
  131.     if (setjmp(st->data.common->exit_jmpbuf))
  132.     return_error(gs_jpeg_log_error(st));
  133.     jpeg_destroy((j_common_ptr) & st->data.compress->cinfo);
  134.     return 0;
  135. }
  136.  
  137.  
  138. /*
  139.  * These routines replace the low-level memory manager of the IJG library.
  140.  * They pass malloc/free calls to the Ghostscript memory manager.
  141.  * Note we do not need these to be declared in any GS header file.
  142.  */
  143.  
  144. private gs_memory_t *
  145. gs_j_common_memory(j_common_ptr cinfo)
  146. {                /*
  147.                  * We use the offset of cinfo in jpeg_compress data here, but we
  148.                  * could equally well have used jpeg_decompress_data.
  149.                  */
  150.     const jpeg_stream_data *sd =
  151.     ((const jpeg_stream_data *)((const byte *)cinfo -
  152.                 offset_of(jpeg_compress_data, cinfo)));
  153.  
  154.     return sd->memory;
  155. }
  156.  
  157. void *
  158. jpeg_get_small(j_common_ptr cinfo, size_t sizeofobject)
  159. {
  160.     gs_memory_t *mem = gs_j_common_memory(cinfo);
  161.  
  162.     return gs_alloc_bytes_immovable(mem, sizeofobject,
  163.                     "JPEG small internal data allocation");
  164. }
  165.  
  166. void
  167. jpeg_free_small(j_common_ptr cinfo, void *object, size_t sizeofobject)
  168. {
  169.     gs_memory_t *mem = gs_j_common_memory(cinfo);
  170.  
  171.     gs_free_object(mem, object, "Freeing JPEG small internal data");
  172. }
  173.  
  174. void FAR *
  175. jpeg_get_large(j_common_ptr cinfo, size_t sizeofobject)
  176. {
  177.     gs_memory_t *mem = gs_j_common_memory(cinfo);
  178.  
  179.     return gs_alloc_bytes_immovable(mem, sizeofobject,
  180.                     "JPEG large internal data allocation");
  181. }
  182.  
  183. void
  184. jpeg_free_large(j_common_ptr cinfo, void FAR * object, size_t sizeofobject)
  185. {
  186.     gs_memory_t *mem = gs_j_common_memory(cinfo);
  187.  
  188.     gs_free_object(mem, object, "Freeing JPEG large internal data");
  189. }
  190.  
  191. long
  192. jpeg_mem_available(j_common_ptr cinfo, long min_bytes_needed,
  193.            long max_bytes_needed, long already_allocated)
  194. {
  195.     return max_bytes_needed;
  196. }
  197.  
  198. void
  199. jpeg_open_backing_store(j_common_ptr cinfo, backing_store_ptr info,
  200.             long total_bytes_needed)
  201. {
  202.     ERREXIT(cinfo, JERR_NO_BACKING_STORE);
  203. }
  204.  
  205. long
  206. jpeg_mem_init(j_common_ptr cinfo)
  207. {
  208.     return 0;            /* just set max_memory_to_use to 0 */
  209. }
  210.  
  211. void
  212. jpeg_mem_term(j_common_ptr cinfo)
  213. {
  214.     /* no work */
  215. }
  216.